def CRC16(buf,length):
    ret=0
    for i in range(0,length):
        b = buf[i]      
        for j in range(0,8):
            if (b^ret)&1:
                ret = (ret>>1) ^ 0xA001
            else:
                ret = (ret>>1)
            b >>= 1           
    return ret

import usb.core

if __name__ == '__main__':
    buf=[0x02,0xAA,0x55,
           0x00,0x00,0x00,0x00,
           0x01,
           0x00,0x00,
           0x00,0x00,0x00,
           0x00,0x00,0x03]
    Crc = CRC16(buf[1:-3],12)
    buf[13] = Crc&0xff
    buf[14] = (Crc>>8)&0xff
    print "CRC16:%x" % Crc
    dev = usb.core.find(idVendor=0x2197, idProduct=0x0001)
    dev.set_configuration()
    dir(dev)
    dev.write(1, buf)        
    input("input:")    

    del dev
